<template>
  <div class="wrapper">
    通告主信息
    <FormPanel ref="infoMain" :list="formList" :formType="type" @formChange="changeHandle" :isSubmitBtn="false" />
    <div>
      主要内容
      <div v-html="content"></div>
    </div>
    附件上传
    <FormPanel ref="infoFile" :list="form" :formType="type" @formChange="changeHandleFile" :isSubmitBtn="false" />
    <div class="dialogBottmView">
      <el-button @click="closeDialog()">关 闭</el-button>
    </div>
  </div>
</template>

<script>
import { authority } from '@/utils/authMixin';
import { dictionary } from '@/utils/dictMixin';
import { addNotification, getNotificationById, updateNotification, publishNotification } from '@/api/admin/notice/dcsNoticeMaintain/index';

export default {
  mixins: [authority, dictionary],
  props: {
    value: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      formList: this.createFormList(),
      form: this.createForm(),
      notificationId: this.value.type === 'show' ? this.value.notificationId : null,
      type: this.value.type,
      dataMain: null, // 第一个表单的数据
      content: null,
      dataInfo: null
    };
  },
  // 页面加载完成 请求接口
  mounted() {
    if (this.notificationId != null) {
      this.initData();
    }
  },
  methods: {
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '通告类别',
          fieldName: 'notificationType',
          placeholder: '请选择',
          itemList: this.createDictList('2561'),
          rules: [{ required: true, message: '请选择通告类别', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '通告主题',
          fieldName: 'notificationTitle',
          placeholder: '请输入',
          initialValue: '',
          rules: [
            { required: true, message: '必填', trigger: 'blur' },
            { max: 200, message: '长度最大50字符', trigger: 'blur' }
          ]
        },
        {
          type: 'INPUT',
          label: '发布部门',
          disabled: true,
          fieldName: 'publishOrgId',
          initialValue: '10011231',
          rules: [{ required: true, message: '请选择部门', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '紧急程度',
          fieldName: 'urgency',
          placeholder: '请选择',
          itemList: this.createDictList('2562'),
          rules: [{ required: true, message: '请选择紧急程度', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '发布人',
          fieldName: 'PUBLISH_PERSON',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          change: this.isAllChange
        },
        {
          type: 'DATE',
          label: '有效期至',
          fieldName: 'endTime',
          placeholder: '请选择',
          valueFormat: 'yyyy-MM-dd',
          initialValue: '',
          rules: [{ required: true, message: '请选择日期', trigger: 'blur' }]
        }
      ];
    },
    createForm() {
      return [
        {
          type: 'UPLOAD_FILE',
          label: '附件',
          disabled: this.type === 'show' ? true : false,
          fieldName: 'caseUrl',
          initialValue: '',
          upload: {
            actionUrl: '/api/file/oss/upload'
          }
        }
      ];
    },
    // 通过ID获取页面详情数据
    async initData() {
      var res = await getNotificationById(this.notificationId);

      if (res.resultCode === 200) {
        this.initPackageData(res);
      } else {
        this.$notify.error({ title: '提示信息', message: res.errMsg || '获取数据异常，请稍后再试！' });
        this.closeDialog();
      }
    },
    // 关闭当前页面操作
    closeDialog(type) {
      this.$emit('close', { visible: false, type: type });
    },
    // 初始化数据封装
    initPackageData(res) {
      this.dataMain = res.data;
      //赋值
      this.formList.forEach(x => {
        x.initialValue = this.dataMain[x.fieldName];
      });

      this.content = res.data.notificationContent;
      this.dataInfo = res.data.url; // TODO 附件回显

      console.log('abc:', res);
    },
    changeHandle(data) {},
    changeHandleFile(data) {}
  }
};
</script>
<style scoped>
.wrapper {
  height: calc(100vh - 110px);
}
</style>
